package com.jackrain.nea.ext.permission;

import com.jackrain.nea.core.schema.SubSystem;
import com.jackrain.nea.core.schema.TableCategory;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.web.query.QuerySession;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author lzm
 * @date 2017-11-17
 */
@Data
public class MenuNode implements Serializable {
    private static final long serialVersionUID = 2621725729572778101L;
    private Long id;
    private MenuNodeType nodeType;
    private MenuType type;
    private String mask;
    private String name;
    private String description;
    private Long ad_subsystem_id;
    private Long ad_tablecategory_id;
    private Long ad_table_id;
    private Long ad_action_id;
    private boolean isdev;
    private Integer orderno;
    private Collection<MenuNode> children = new ArrayList<>();

    public static MenuNode createRootNode(){
        MenuNode result = new MenuNode();
        result.setNodeType(MenuNodeType.ROOT);
        result.setId(-1L);
        result.setName("全部");
        result.setDescription("全部");
        return result;
    }

    public static MenuNode createSubSystemNode(QuerySession session, Long subSystemId){
        MenuNode node = new MenuNode();
        node.setNodeType(MenuNodeType.SUB_SYSTEM);
        node.setType(MenuType.UNKNOWN);
        SubSystem subSystem = session.getTableManager().getSubSystem(subSystemId.intValue());
        node.setAd_subsystem_id(subSystemId);
        node.setId(subSystemId);
        node.setOrderno(subSystem.getOrderno());
        node.setMask(TableMaskHelper.ONLY_VIEW_MASK);
        node.setName(subSystem.getName());
        node.setDescription(subSystem.getDescription(session.getLocale()));
        return node;
    }

    public static MenuNode createTableCategoryNode(QuerySession session, Long subSystemId, Long tableCategoryId){
        MenuNode node = new MenuNode();
        node.setNodeType(MenuNodeType.TABLE_CATEGORY);
        node.setType(MenuType.UNKNOWN);
        TableCategory category = session.getTableManager().getTableCategory(tableCategoryId.intValue());
        node.setAd_subsystem_id(subSystemId);
        node.setAd_tablecategory_id(tableCategoryId);
        node.setId(tableCategoryId);
        node.setOrderno(category.getOrder());
        node.setMask(TableMaskHelper.ONLY_VIEW_MASK);
        node.setName(category.getName());
        node.setDescription(category.getDescription(session.getLocale()));
        return node;
    }

    public String fetchRealId() {
        return fetchRealId(getNodeType(), getId());
    }

    public static String fetchRealId(MenuNodeType nodeType, Long id){
        switch (nodeType) {
            case ROOT:
                return "R_" + id;
            case SUB_SYSTEM:
                return "S_" + id;
            case TABLE_CATEGORY:
                return "C_" + id;
            case MENU:
                return "M_" + id;
            default:
                throw new NDSException(String.format("MenuNodeType不存在值%d", nodeType.getValue()));
        }
    }

    public String fetchRealTmId(){
        MenuType type = getType();
        Long tmId;
        switch (type) {
            case TABLE:
                tmId = getAd_table_id();
                break;
            case ACTION:
                tmId = getAd_action_id();
                break;
            default:
                throw new NDSException(String.format("NodeType不存在值%d", type.getValue()));
        }
        return fetchRealTmId(type, tmId);
    }

    public static String fetchRealTmId(MenuType type, Long tmId){
        return fetchTmPrefix(type) + tmId;
    }

    public String fetchRealTmName(){
        return fetchRealTmName(getType(), getName());
    }

    public static String fetchRealTmName(MenuType type, String tmName){
        return fetchTmPrefix(type) + tmName;
    }

    private static String fetchTmPrefix(MenuType type){
        switch (type) {
            case TABLE:
                return "MT_";
            case ACTION:
                return "MA_";
            default:
                throw new NDSException(String.format("NodeType不存在值%d", type.getValue()));
        }
    }

    public static boolean isRootNode(MenuNode node){
        return node.getNodeType() == MenuNodeType.ROOT;
    }

    public MenuNode copy() {
        MenuNode result = new MenuNode();
        result.setId(this.getId());
        result.setNodeType(this.getNodeType());
        result.setType(this.getType());
        result.setMask(this.getMask());
        result.setName(this.getName());
        result.setDescription(this.getDescription());
        result.setAd_subsystem_id(this.getAd_subsystem_id());
        result.setAd_tablecategory_id(this.getAd_tablecategory_id());
        result.setAd_table_id(this.getAd_table_id());
        result.setAd_action_id(this.getAd_action_id());
        result.setOrderno(this.getOrderno());
        return result;
    }
}
